package cn.hysh.appium.testcases;

import cn.hysh.appium.base.AndroidDriverBase;
import cn.hysh.appium.base.ChPath;
import cn.hysh.appium.datadriver.ExcelUtil;
import cn.hysh.appium.page.EditPersonInfo;
import cn.hysh.appium.page.Home;
import cn.hysh.appium.page.Login;
import cn.hysh.appium.testngdemo.Assertion;
import cn.hysh.appium.util.ProUtil;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import org.testng.Assert;
import org.testng.annotations.*;

public class MmTest {
	AndroidDriverBase driver;
	Assertion myAssert;
	@Parameters({"udid","port"})
	@BeforeClass
	public void beforeClass(String udid,String port){
		ProUtil p=new ProUtil(ChPath.globalPath);
		String server=p.getPro("server");
		//String port="4490";
		String capsPath=ChPath.capsPath;
		//String udid="127.0.0.1:62001";
		String input="com.example.android.softkeyboard/.SoftKeyboard";
		try {
			driver=new AndroidDriverBase(server, port, capsPath, udid, input);
			myAssert=new Assertion(driver);
			driver.implicitlyWaitDefault();
		} catch (Exception e) {
			System.out.println("错误的！");
		}
	}
	@DataProvider
	public Object[][] loginData() throws Exception{
		Object[][] ob=ExcelUtil.getTestData("C:\\Users\\Administrator\\Desktop/test.xlsx", "Sheet1");
		return ob;
	}
	@Test(dataProvider="loginData")
	public void t001_login(String caseNumber,String caseName,String username,String password,String assertValue,String filename) throws Exception{
		ExcelUtil eu=new ExcelUtil("C:\\Users\\Administrator\\Desktop/test.xlsx", "Sheet1");
		Login l=new Login(driver);
		Home home=l.loginTest(username,password);
		try {
			myAssert.assertEquals(driver.getPageSouce().contains(assertValue), true, filename);
			eu.setCellData(Integer.valueOf(caseNumber), eu.getLastColumnNum(), "测试通过");
		} catch (Error e) {
			eu.setCellData(Integer.valueOf(caseNumber), eu.getLastColumnNum(), "测试失败");
			Assert.fail(e.getMessage());
		}

	}
	@Test(enabled=false)
	public void t002_search(){
		Home home=new Home(driver);
		Boolean flag=home.searchTest("test");
		myAssert.assertEquals(flag==true, true, "searcherror");

	}
	@Test(enabled=false)
	public void t003_personInfo(){
		EditPersonInfo edinfo=new EditPersonInfo(driver);
		boolean flag=edinfo.modifyPersonInfo();
		myAssert.assertEquals(flag, true, "personinfoerror");
		System.out.println(1);
		
	}
	
	@AfterMethod
	public void restart(){
		String appPackge=(String)driver.getCapabilities().getCapability(AndroidMobileCapabilityType.APP_PACKAGE);
		String appActivity=(String)driver.getCapabilities().getCapability(AndroidMobileCapabilityType.APP_ACTIVITY);
		driver.startActivity(appPackge,appActivity);
	}
	
	@AfterClass
	public void afterClass(){
		driver.resetApp();
		driver.quit();
	}

}
